package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;




    @Override
    @Transactional
    public void insertMeal(SetmealDTO setmealDTO) {
        //查询setmeal表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insertMeal(setmeal);
        // 2. 添加套餐对应的菜品信息 setmeal_dish 多对多
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null) {
            for (SetmealDish setmealDish : setmealDishes) {
                //中间表设置关联关系
                setmealDish.setSetmealId(setmeal.getId());
            }
            //新增套餐菜品信息对应setmeal_dish表
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    @Override
    public PageResult getSetmealPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        //1.开启分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealPageQueryDTO, setmeal);
        //2.调用套餐mapper对象查询数据
        Page<SetmealVO> setmealslist = setmealMapper.getSetmealPage(setmeal);
        //3.获取查询的当前页和总条数
        PageResult pageResult = new PageResult();
        pageResult.setRecords(setmealslist.getResult());
        pageResult.setTotal(setmealslist.getTotal());

        return pageResult;
    }

    /**
     * 套餐批量删除 删除前首先的判断套餐状态是否为起售
     * 否则抛出异常
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBySetmealIds(List<Long> ids) {
        //将id和启售状态当成条件 聚合查询
        if (ids!=null && ids.size()>0){
            int count =  setmealMapper.countBySetmealStatus(ids, StatusConstant.ENABLE);
            if(count>0){
                throw  new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        setmealMapper.deleteBySetmealIds(ids);
        for (Long setmealDishId : ids) {
            setmealMapper.deleteBySetmealDishId(setmealDishId);

        }

    }

    /**
     * 回显
     * @param id
     * @return
     */
    @Override
    public SetmealVO getBySetmealId(Long id) {

        return setmealMapper.getBySetmealId(id);
    }

    /**
     * 修改 setmealDish 如果要修改需要先将setmealdish表数据删除 再新增
     * @param setmealDTO
     */
    @Override
    public void updateSetmealWithDish(SetmealDTO setmealDTO) {
        //1.将请求数据放到数据库表对应实体类
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //2.调用mapper修改
        setmealMapper.updateSetmealWithDish(setmeal);
        //3.修改套餐关联菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if(setmealDishes!=null && setmealDishes.size()>0){
            setmealDishMapper.deleteSetmealDish(setmealDTO.getId());
            //遍历设置setmeal_id关联关系
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealDTO.getId());
            }
            setmealDishMapper.insertBatch(setmealDishes);

        }

    }

    /**
     * 启售停售
     * @param status
     * @param id
     */
    @Override
    public void StartOrStop(Integer status, Long id) {

        if(status.equals(StatusConstant.ENABLE)){
            int count = setmealDishMapper.getDishIdWithDIsh(StatusConstant.DISABLE, id);
            if (count > 0) {
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }

        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();

        setmealMapper.updateSetmealWithDish(setmeal);

    }


}
